#!/usr/bin/python

###############################
## A base function for plots ##
###############################

#######################################################      
## Set PyRoot Environment and PlotConfig Path first ###
#######################################################
import sys
ROOTSYS = '/afs/atlas.umich.edu/opt/root/lib'
sys.path.append(ROOTSYS)
PlotConfig='../'
PlotConfig2='.'
sys.path.append(PlotConfig)
sys.path.append(PlotConfig2)

#####################
## Import Module  ###
#####################
import array
import os
import glob
from math import sqrt,fabs,sin
from ROOT import TFile,TTree,TChain,TBranch,TH1,TH1F,TList
from ROOT import TLorentzVector
from ROOT import THStack,TCanvas,TLegend,TColor,TPaveText
from ROOT import gStyle,gDirectory

####################
## Import Template #
####################
from plot_template import plot_template
from plot_template_4lep import plot_template_combined

############################
## Load Atlas Plot Style ###            
############################

from plot_style import atlas_style
atlas_style()

###############
## Plotting ###
###############
RatioSize=0.2
RatioErr=-1
RatioMin,RatioMax=0,3
RatioAdj=0

ShowMCErr=1
MCErreeee=0.062
MCErreemm=0.044
MCErrmmee=0.044
MCErrmmmm=0.039
MCErr4l=0.044

plot_config='plot_configure'


#ATLAS='ATLAS For Approval'
ATLAS='ALTAS Preliminary'
figFormat='all'
ch_xleg=0.57

####### SF, ele eta/phi
histoDir='NOM'
##### lepton Eta
Mmin, Mmax=-2.5, 2.5
yscale=2.5
Rebin=25
binSize=Rebin*0.02
Events=str('Events / %.1f' % binSize)
hname=histoDir+'_eeee_SF_ElEta'
hxt='#eta_{e}'
MCErr=MCErreeee
plot_template(histodir=histoDir,hname=hname,hxt=hxt,hyt=Events,hrebin=Rebin,xmin=Mmin,xmax=Mmax,yscale=yscale,ratio=RatioSize,ratio_min=RatioMin,ratio_max=RatioMax,ratio_adj=RatioAdj,ratio_err=RatioErr,show_mcerr=ShowMCErr,percent_mcerr=MCErr,showATLAS=ATLAS,figformat=figFormat,ch_xleg=ch_xleg)
hname=histoDir+'_eeee_SF_ElClEta'
hxt='#eta_{e}'
MCErr=MCErreeee
plot_template(histodir=histoDir,hname=hname,hxt=hxt,hyt=Events,hrebin=Rebin,xmin=Mmin,xmax=Mmax,yscale=yscale,ratio=RatioSize,ratio_min=RatioMin,ratio_max=RatioMax,ratio_adj=RatioAdj,ratio_err=RatioErr,show_mcerr=ShowMCErr,percent_mcerr=MCErr,showATLAS=ATLAS,figformat=figFormat,ch_xleg=ch_xleg)

yscale=3.0
hname=histoDir+'_eemm_SF_ElEta'
hxt='#eta_{e}'
MCErr=MCErreemm
plot_template(histodir=histoDir,hname=hname,hxt=hxt,hyt=Events,hrebin=Rebin,xmin=Mmin,xmax=Mmax,yscale=yscale,ratio=RatioSize,ratio_min=RatioMin,ratio_max=RatioMax,ratio_adj=RatioAdj,ratio_err=RatioErr,show_mcerr=ShowMCErr,percent_mcerr=MCErr,showATLAS=ATLAS,figformat=figFormat,ch_xleg=ch_xleg)
hname=histoDir+'_eemm_SF_ElClEta'
hxt='#eta_{e}'
MCErr=MCErreemm
plot_template(histodir=histoDir,hname=hname,hxt=hxt,hyt=Events,hrebin=Rebin,xmin=Mmin,xmax=Mmax,yscale=yscale,ratio=RatioSize,ratio_min=RatioMin,ratio_max=RatioMax,ratio_adj=RatioAdj,ratio_err=RatioErr,show_mcerr=ShowMCErr,percent_mcerr=MCErr,showATLAS=ATLAS,figformat=figFormat,ch_xleg=ch_xleg)

hname=histoDir+'_mmee_SF_ElEta'
hxt='#eta_{e}'
MCErr=MCErrmmee
plot_template(histodir=histoDir,hname=hname,hxt=hxt,hyt=Events,hrebin=Rebin,xmin=Mmin,xmax=Mmax,yscale=yscale,ratio=RatioSize,ratio_min=RatioMin,ratio_max=RatioMax,ratio_adj=RatioAdj,ratio_err=RatioErr,show_mcerr=ShowMCErr,percent_mcerr=MCErr,showATLAS=ATLAS,figformat=figFormat,ch_xleg=ch_xleg)
hname=histoDir+'_mmee_SF_ElClEta'
hxt='#eta_{e}'
MCErr=MCErrmmee
plot_template(histodir=histoDir,hname=hname,hxt=hxt,hyt=Events,hrebin=Rebin,xmin=Mmin,xmax=Mmax,yscale=yscale,ratio=RatioSize,ratio_min=RatioMin,ratio_max=RatioMax,ratio_adj=RatioAdj,ratio_err=RatioErr,show_mcerr=ShowMCErr,percent_mcerr=MCErr,showATLAS=ATLAS,figformat=figFormat,ch_xleg=ch_xleg)

yscale=2.5
hname=histoDir+'_eeee_SF_ElPhi'
hxt='#phi_{e}'
MCErr=MCErreeee
plot_template(histodir=histoDir,hname=hname,hxt=hxt,hyt=Events,hrebin=Rebin,xmin=Mmin,xmax=Mmax,yscale=yscale,ratio=RatioSize,ratio_min=RatioMin,ratio_max=RatioMax,ratio_adj=RatioAdj,ratio_err=RatioErr,show_mcerr=ShowMCErr,percent_mcerr=MCErr,showATLAS=ATLAS,figformat=figFormat,ch_xleg=ch_xleg)
hname=histoDir+'_eeee_SF_ElClPhi'
hxt='#phi_{e}'
MCErr=MCErreeee
plot_template(histodir=histoDir,hname=hname,hxt=hxt,hyt=Events,hrebin=Rebin,xmin=Mmin,xmax=Mmax,yscale=yscale,ratio=RatioSize,ratio_min=RatioMin,ratio_max=RatioMax,ratio_adj=RatioAdj,ratio_err=RatioErr,show_mcerr=ShowMCErr,percent_mcerr=MCErr,showATLAS=ATLAS,figformat=figFormat,ch_xleg=ch_xleg)

yscale=3.0
hname=histoDir+'_eemm_SF_ElPhi'
hxt='#phi_{e}'
MCErr=MCErreemm
plot_template(histodir=histoDir,hname=hname,hxt=hxt,hyt=Events,hrebin=Rebin,xmin=Mmin,xmax=Mmax,yscale=yscale,ratio=RatioSize,ratio_min=RatioMin,ratio_max=RatioMax,ratio_adj=RatioAdj,ratio_err=RatioErr,show_mcerr=ShowMCErr,percent_mcerr=MCErr,showATLAS=ATLAS,figformat=figFormat,ch_xleg=ch_xleg)
hname=histoDir+'_eemm_SF_ElClPhi'
hxt='#phi_{e}'
MCErr=MCErreemm
plot_template(histodir=histoDir,hname=hname,hxt=hxt,hyt=Events,hrebin=Rebin,xmin=Mmin,xmax=Mmax,yscale=yscale,ratio=RatioSize,ratio_min=RatioMin,ratio_max=RatioMax,ratio_adj=RatioAdj,ratio_err=RatioErr,show_mcerr=ShowMCErr,percent_mcerr=MCErr,showATLAS=ATLAS,figformat=figFormat,ch_xleg=ch_xleg)

hname=histoDir+'_mmee_SF_ElPhi'
hxt='#phi_{e}'
MCErr=MCErrmmee
plot_template(histodir=histoDir,hname=hname,hxt=hxt,hyt=Events,hrebin=Rebin,xmin=Mmin,xmax=Mmax,yscale=yscale,ratio=RatioSize,ratio_min=RatioMin,ratio_max=RatioMax,ratio_adj=RatioAdj,ratio_err=RatioErr,show_mcerr=ShowMCErr,percent_mcerr=MCErr,showATLAS=ATLAS,figformat=figFormat,ch_xleg=ch_xleg)
hname=histoDir+'_mmee_SF_ElClPhi'
hxt='#phi_{e}'
MCErr=MCErrmmee
plot_template(histodir=histoDir,hname=hname,hxt=hxt,hyt=Events,hrebin=Rebin,xmin=Mmin,xmax=Mmax,yscale=yscale,ratio=RatioSize,ratio_min=RatioMin,ratio_max=RatioMax,ratio_adj=RatioAdj,ratio_err=RatioErr,show_mcerr=ShowMCErr,percent_mcerr=MCErr,showATLAS=ATLAS,figformat=figFormat,ch_xleg=ch_xleg)


##### SF, mu eta/phi
Mmin, Mmax=-3.2, 3.3
Rebin=20
binSize=Rebin*0.02
Events=str('Events / %.1f' % binSize)
yscale=2.5
hname=histoDir+'_mmmm_SF_MuEta'
hxt='#eta_{#mu}'
MCErr=MCErrmmmm
plot_template(histodir=histoDir,hname=hname,hxt=hxt,hyt=Events,hrebin=Rebin,xmin=Mmin,xmax=Mmax,yscale=yscale,ratio=RatioSize,ratio_min=RatioMin,ratio_max=RatioMax,ratio_adj=RatioAdj,ratio_err=RatioErr,show_mcerr=ShowMCErr,percent_mcerr=MCErr,showATLAS=ATLAS,figformat=figFormat,ch_xleg=ch_xleg)
hname=histoDir+'_mmmm_SF_MuPhi'
hxt='#phi_{#mu}'
MCErr=MCErrmmmm
plot_template(histodir=histoDir,hname=hname,hxt=hxt,hyt=Events,hrebin=Rebin,xmin=Mmin,xmax=Mmax,yscale=yscale,ratio=RatioSize,ratio_min=RatioMin,ratio_max=RatioMax,ratio_adj=RatioAdj,ratio_err=RatioErr,show_mcerr=ShowMCErr,percent_mcerr=MCErr,showATLAS=ATLAS,figformat=figFormat,ch_xleg=ch_xleg)

hname=histoDir+'_eemm_SF_MuEta'
hxt='#eta_{#mu}'
MCErr=MCErreemm
plot_template(histodir=histoDir,hname=hname,hxt=hxt,hyt=Events,hrebin=Rebin,xmin=Mmin,xmax=Mmax,yscale=yscale,ratio=RatioSize,ratio_min=RatioMin,ratio_max=RatioMax,ratio_adj=RatioAdj,ratio_err=RatioErr,show_mcerr=ShowMCErr,percent_mcerr=MCErr,showATLAS=ATLAS,figformat=figFormat,ch_xleg=ch_xleg)
hname=histoDir+'_eemm_SF_MuPhi'
hxt='#phi_{#mu}'
MCErr=MCErreemm
plot_template(histodir=histoDir,hname=hname,hxt=hxt,hyt=Events,hrebin=Rebin,xmin=Mmin,xmax=Mmax,yscale=yscale,ratio=RatioSize,ratio_min=RatioMin,ratio_max=RatioMax,ratio_adj=RatioAdj,ratio_err=RatioErr,show_mcerr=ShowMCErr,percent_mcerr=MCErr,showATLAS=ATLAS,figformat=figFormat,ch_xleg=ch_xleg)

hname=histoDir+'_mmee_SF_MuEta'
hxt='#eta_{#mu}'
MCErr=MCErrmmee
plot_template(histodir=histoDir,hname=hname,hxt=hxt,hyt=Events,hrebin=Rebin,xmin=Mmin,xmax=Mmax,yscale=yscale,ratio=RatioSize,ratio_min=RatioMin,ratio_max=RatioMax,ratio_adj=RatioAdj,ratio_err=RatioErr,show_mcerr=ShowMCErr,percent_mcerr=MCErr,showATLAS=ATLAS,figformat=figFormat,ch_xleg=ch_xleg)
hname=histoDir+'_mmee_SF_MuPhi'
hxt='#phi_{#mu}'
MCErr=MCErrmmee
plot_template(histodir=histoDir,hname=hname,hxt=hxt,hyt=Events,hrebin=Rebin,xmin=Mmin,xmax=Mmax,yscale=yscale,ratio=RatioSize,ratio_min=RatioMin,ratio_max=RatioMax,ratio_adj=RatioAdj,ratio_err=RatioErr,show_mcerr=ShowMCErr,percent_mcerr=MCErr,showATLAS=ATLAS,figformat=figFormat,ch_xleg=ch_xleg)

